package com.ruoyi.cderp.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.alibaba.druid.sql.transform.TableMapping;
import com.ruoyi.cderp.domain.*;
import com.ruoyi.cderp.mapper.*;
import com.ruoyi.cderp.service.ICustomerSettleCheckSheetService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Random;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.service.ICustomerSettleSheetService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 客户结算单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-11
 */
@Service
public class CustomerSettleSheetServiceImpl implements ICustomerSettleSheetService
{
    //客户结算单Mapper
    @Autowired
    private CustomerSettleSheetMapper customerSettleSheetMapper;

    //客户对账单Service
    @Autowired
    private ICustomerSettleCheckSheetService customerSettleCheckSheetService;

    //客户对账单Mapper
    @Autowired
    private CustomerSettleCheckSheetMapper customerSettleCheckSheetMapper;

    //客户费用单Mapper
    @Autowired
    private CustomerSettleFeeSheetMapper customerSettleFeeSheetMapper;

    //客户预结算单Mapper
    @Autowired
    private CustomerSettlePreSheetMapper customerSettlePreSheetMapper;

    //销售出库单Mapper
    @Autowired
    private TblSaleOutSheetMapper tblSaleOutSheetMapper;

    //销售退单Mapper
    @Autowired
    private TblSaleReturnMapper  tblSaleReturnMapper;

    /**
     * 查询客户结算单
     *
     * @param id 客户结算单主键
     * @return 客户结算单
     */
    @Override
    public CustomerSettleSheet selectCustomerSettleSheetById(String id)
    {
        CustomerSettleSheet customerSettleSheet = customerSettleSheetMapper.selectCustomerSettleSheetById(id);
        customerSettleSheet.setCustomerSettleSheetDetailList(customerSettleSheetMapper.selectCustomerSettleDetail(id));
        return customerSettleSheet;
    }

    /**
     * 查询客户结算单列表
     *
     * @param customerSettleSheet 客户结算单
     * @return 客户结算单
     */
    @Override
    public List<CustomerSettleSheet> selectCustomerSettleSheetList(CustomerSettleSheet customerSettleSheet)
    {
        return customerSettleSheetMapper.selectCustomerSettleSheetList(customerSettleSheet);
    }

    /**
     * 新增客户结算单
     *
     * @param customerSettleSheet 客户结算单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCustomerSettleSheet(CustomerSettleSheet customerSettleSheet)
    {
        customerSettleSheet.setCreateTime(DateUtils.getNowDate());

        // 获取当前登录用户对象
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //创建人编号
        customerSettleSheet.setCreateById(loginUser.getUser().getUserId().toString());
        //创建人名称
        customerSettleSheet.setCreateBy(loginUser.getUser().getUserName());

        int rows = customerSettleSheetMapper.insertCustomerSettleSheet(customerSettleSheet);
        insertCustomerSettleSheetDetail(customerSettleSheet);
        return rows;
    }

    /**
     * 修改客户结算单
     *
     * @param customerSettleSheet 客户结算单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCustomerSettleSheet(CustomerSettleSheet customerSettleSheet)
    {
        customerSettleSheet.setUpdateTime(DateUtils.getNowDate());

        // 获取当前登录用户对象
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //修改人编号
        customerSettleSheet.setUpdateById(loginUser.getUser().getUserId().toString());
        //修改人名称
        customerSettleSheet.setUpdateBy(loginUser.getUser().getUserName());


        //根据客户结算单id查询结算单详情
        List<CustomerSettleSheetDetail> customerSettleSheetDetails = customerSettleSheetMapper.selectCustomerSettleDetail(customerSettleSheet.getId());
        //循环遍历详情
        for (CustomerSettleSheetDetail customerSettleSheetDetail : customerSettleSheetDetails)
        {
            //实例化一个对账对象
            CustomerSettleCheckSheet customerSettleCheckSheet = new CustomerSettleCheckSheet();
            //赋值对账单状态
            customerSettleCheckSheet.setSettleStatus(Long.valueOf(0));
            //赋值对账单编号
            customerSettleCheckSheet.setId(customerSettleSheetDetail.getBizId());
            //调用对账修改接口
            customerSettleCheckSheetMapper.updateCustomerSettleCheckSheet(customerSettleCheckSheet);

            //调用查询对账详情接口
            CustomerSettleCheckSheet customerSettleCheckSheetDetails = customerSettleCheckSheetService.selectCustomerSettleCheckSheetById(customerSettleSheetDetail.getBizId());
            //循环遍历对账详情
            for(CustomerSettleCheckSheetDetail customerSettleCheckSheetDetail:customerSettleCheckSheetDetails.getCustomerSettleCheckSheetDetailList()){
                //根据对账单详情中类型判断修改相对应的结算状态
                if(customerSettleCheckSheetDetail.getBizType()==1){
                    //修改客户应收款单的结算状态
                    CustomerSettleFeeSheet customerSettleFeeSheet = new CustomerSettleFeeSheet();
                    customerSettleFeeSheet.setSettleStatus(Long.valueOf(0));
                    customerSettleFeeSheet.setId(customerSettleCheckSheetDetail.getBizId());
                    customerSettleFeeSheetMapper.updateCustomerSettleFeeSheet(customerSettleFeeSheet);
                     //根据对账的详情中的客户收款单id查询收款单详情
                     List<CustomerSettleFeeSheetDetail> FeeSheetDetail = customerSettleFeeSheetMapper.selectFeeSheetDetailIn(customerSettleCheckSheetDetail.getBizId());
                     //循环客户收款单详情修改结算状态
                     for(CustomerSettleFeeSheetDetail customerSettleFeeSheetDetail:FeeSheetDetail){
                         TblSaleOutSheet tblSaleOutSheet = new TblSaleOutSheet();
                         tblSaleOutSheet.setSettleStatus(Integer.valueOf(0));
                         tblSaleOutSheet.setId(customerSettleFeeSheetDetail.getOrderId());
                         tblSaleOutSheetMapper.updateTblSaleOutSheet(tblSaleOutSheet);
                     }
                }
                if(customerSettleCheckSheetDetail.getBizType()==2){
                    CustomerSettlePreSheet customerSettlePreSheet = new CustomerSettlePreSheet();
                    customerSettlePreSheet.setSettleStatus(Long.valueOf(0));
                    customerSettlePreSheet.setId(customerSettleCheckSheetDetail.getBizId());
                    customerSettlePreSheetMapper.updateCustomerSettlePreSheet(customerSettlePreSheet);

                    List<CustomerSettlePreSheetDetail> PreSheetDetail = customerSettlePreSheetMapper.selectPreSheetDetail(customerSettleCheckSheetDetail.getBizId());
                    for(CustomerSettlePreSheetDetail customerSettlePreSheetDetail:PreSheetDetail){
                        TblSaleReturn tblSaleReturn = new TblSaleReturn();
                        tblSaleReturn.setSettleStatus(Integer.valueOf(0));
                        tblSaleReturn.setId(customerSettlePreSheetDetail.getOrderId());
                        tblSaleReturnMapper.updateTblSaleReturn(tblSaleReturn);
                    }
                }
            }
        }
        customerSettleSheetMapper.deleteCustomerSettleSheetDetailBySheetId(customerSettleSheet.getId());
        insertCustomerSettleSheetDetail(customerSettleSheet);
        if(customerSettleSheet.getStatus() == 3){
            //根据客户结算单id查询结算单详情
            List<CustomerSettleSheetDetail> customerSettleSheetDetails1 = customerSettleSheetMapper.selectCustomerSettleDetail(customerSettleSheet.getId());
            //循环遍历详情
            for (CustomerSettleSheetDetail customerSettleSheetDetail : customerSettleSheetDetails1)
            {
                //实例化一个对账对象
                CustomerSettleCheckSheet customerSettleCheckSheet = new CustomerSettleCheckSheet();
                //赋值对账单状态
                customerSettleCheckSheet.setSettleStatus(Long.valueOf(2));
                //赋值对账单编号
                customerSettleCheckSheet.setId(customerSettleSheetDetail.getBizId());
                //调用对账修改接口
                customerSettleCheckSheetMapper.updateCustomerSettleCheckSheet(customerSettleCheckSheet);


                //调用查询对账详情接口
                CustomerSettleCheckSheet customerSettleCheckSheetDetails = customerSettleCheckSheetService.selectCustomerSettleCheckSheetById(customerSettleSheetDetail.getBizId());
                //循环遍历对账详情
                for(CustomerSettleCheckSheetDetail customerSettleCheckSheetDetail:customerSettleCheckSheetDetails.getCustomerSettleCheckSheetDetailList()){
                    //根据对账单详情中类型判断修改相对应的结算状态
                    if(customerSettleCheckSheetDetail.getBizType()==1){
                        //修改客户应收款单的结算状态
                        CustomerSettleFeeSheet customerSettleFeeSheet = new CustomerSettleFeeSheet();
                        customerSettleFeeSheet.setSettleStatus(Long.valueOf(2));
                        customerSettleFeeSheet.setId(customerSettleCheckSheetDetail.getBizId());
                        customerSettleFeeSheetMapper.updateCustomerSettleFeeSheet(customerSettleFeeSheet);
                        //根据对账的详情中的客户收款单id查询收款单详情
                        List<CustomerSettleFeeSheetDetail> FeeSheetDetail = customerSettleFeeSheetMapper.selectFeeSheetDetailIn(customerSettleCheckSheetDetail.getBizId());
                        //循环客户收款单详情修改结算状态
                        for(CustomerSettleFeeSheetDetail customerSettleFeeSheetDetail:FeeSheetDetail){
                            TblSaleOutSheet tblSaleOutSheet = new TblSaleOutSheet();
                            tblSaleOutSheet.setSettleStatus(Integer.valueOf(2));
                            tblSaleOutSheet.setId(customerSettleFeeSheetDetail.getOrderId());
                            tblSaleOutSheetMapper.updateTblSaleOutSheet(tblSaleOutSheet);
                        }
                    }
                    if(customerSettleCheckSheetDetail.getBizType()==2){
                        CustomerSettlePreSheet customerSettlePreSheet = new CustomerSettlePreSheet();
                        customerSettlePreSheet.setSettleStatus(Long.valueOf(2));
                        customerSettlePreSheet.setId(customerSettleCheckSheetDetail.getBizId());
                        customerSettlePreSheetMapper.updateCustomerSettlePreSheet(customerSettlePreSheet);

                        List<CustomerSettlePreSheetDetail> PreSheetDetail = customerSettlePreSheetMapper.selectPreSheetDetail(customerSettleCheckSheetDetail.getBizId());
                        for(CustomerSettlePreSheetDetail customerSettlePreSheetDetail:PreSheetDetail){
                            TblSaleReturn tblSaleReturn = new TblSaleReturn();
                            tblSaleReturn.setSettleStatus(Integer.valueOf(2));
                            tblSaleReturn.setId(customerSettlePreSheetDetail.getOrderId());
                            tblSaleReturnMapper.updateTblSaleReturn(tblSaleReturn);
                        }
                    }
                }
            }
        }
        return customerSettleSheetMapper.updateCustomerSettleSheet(customerSettleSheet);
    }

    /**
     * 批量删除客户结算单
     *
     * @param ids 需要删除的客户结算单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCustomerSettleSheetByIds(String[] ids)
    {
        //根据客户结算单id查询结算单详情
        List<CustomerSettleSheetDetail> customerSettleSheetDetails = customerSettleSheetMapper.selectCustomerSettleDetail(ids[0]);
        //循环遍历详情
        for (CustomerSettleSheetDetail customerSettleSheetDetail : customerSettleSheetDetails)
        {
            //实例化一个对账对象
            CustomerSettleCheckSheet customerSettleCheckSheet = new CustomerSettleCheckSheet();
            //赋值对账单状态
            customerSettleCheckSheet.setSettleStatus(Long.valueOf(0));
            //赋值对账单编号
            customerSettleCheckSheet.setId(customerSettleSheetDetail.getBizId());
            //调用对账修改接口
            customerSettleCheckSheetMapper.updateCustomerSettleCheckSheet(customerSettleCheckSheet);



            //调用查询对账详情接口
            CustomerSettleCheckSheet customerSettleCheckSheetDetails = customerSettleCheckSheetService.selectCustomerSettleCheckSheetById(customerSettleSheetDetail.getBizId());
            //循环遍历对账详情
            for(CustomerSettleCheckSheetDetail customerSettleCheckSheetDetail:customerSettleCheckSheetDetails.getCustomerSettleCheckSheetDetailList()){
                //根据对账单详情中类型判断修改相对应的结算状态
                if(customerSettleCheckSheetDetail.getBizType()==1){
                    //修改客户应收款单的结算状态
                    CustomerSettleFeeSheet customerSettleFeeSheet = new CustomerSettleFeeSheet();
                    customerSettleFeeSheet.setSettleStatus(Long.valueOf(0));
                    customerSettleFeeSheet.setId(customerSettleCheckSheetDetail.getBizId());
                    customerSettleFeeSheetMapper.updateCustomerSettleFeeSheet(customerSettleFeeSheet);
                    //根据对账的详情中的客户收款单id查询收款单详情
                    List<CustomerSettleFeeSheetDetail> FeeSheetDetail = customerSettleFeeSheetMapper.selectFeeSheetDetailIn(customerSettleCheckSheetDetail.getBizId());
                    //循环客户收款单详情修改结算状态
                    for(CustomerSettleFeeSheetDetail customerSettleFeeSheetDetail:FeeSheetDetail){
                        TblSaleOutSheet tblSaleOutSheet = new TblSaleOutSheet();
                        tblSaleOutSheet.setSettleStatus(Integer.valueOf(0));
                        tblSaleOutSheet.setId(customerSettleFeeSheetDetail.getOrderId());
                        tblSaleOutSheetMapper.updateTblSaleOutSheet(tblSaleOutSheet);
                    }
                }
                if(customerSettleCheckSheetDetail.getBizType()==2){
                    CustomerSettlePreSheet customerSettlePreSheet = new CustomerSettlePreSheet();
                    customerSettlePreSheet.setSettleStatus(Long.valueOf(0));
                    customerSettlePreSheet.setId(customerSettleCheckSheetDetail.getBizId());
                    customerSettlePreSheetMapper.updateCustomerSettlePreSheet(customerSettlePreSheet);

                    List<CustomerSettlePreSheetDetail> PreSheetDetail = customerSettlePreSheetMapper.selectPreSheetDetail(customerSettleCheckSheetDetail.getBizId());
                    for(CustomerSettlePreSheetDetail customerSettlePreSheetDetail:PreSheetDetail){
                        TblSaleReturn tblSaleReturn = new TblSaleReturn();
                        tblSaleReturn.setSettleStatus(Integer.valueOf(0));
                        tblSaleReturn.setId(customerSettlePreSheetDetail.getOrderId());
                        tblSaleReturnMapper.updateTblSaleReturn(tblSaleReturn);
                    }
                }
            }
        }
        customerSettleSheetMapper.deleteCustomerSettleSheetDetailBySheetIds(ids);
        return customerSettleSheetMapper.deleteCustomerSettleSheetByIds(ids);
    }

    /**
     * 删除客户结算单信息
     *
     * @param id 客户结算单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCustomerSettleSheetById(String id)
    {
        customerSettleSheetMapper.deleteCustomerSettleSheetDetailBySheetId(id);
        return customerSettleSheetMapper.deleteCustomerSettleSheetById(id);
    }

    //查询指定客户相对应的对账信息
    @Override
    public List<CustomerSettleSheetDetail> selectFeeSheetDetailList(Date startDate, Date endDate, String customerId) {
        List<CustomerSettleSheetDetail> list = new ArrayList<>();
        List<CustomerSettleSheetDetail> customerSettleFeeSheets = customerSettleSheetMapper.selectCustomerSettleCheckList(startDate, endDate, customerId);
        list.addAll(customerSettleFeeSheets);
        return list;
    }

    /**
     * 新增客户结算单明细信息
     *
     * @param customerSettleSheet 客户结算单对象
     */
    public void insertCustomerSettleSheetDetail(CustomerSettleSheet customerSettleSheet)
    {
        List<CustomerSettleSheetDetail> customerSettleSheetDetailList = customerSettleSheet.getCustomerSettleSheetDetailList();
        String id = customerSettleSheet.getId();
        if (StringUtils.isNotNull(customerSettleSheetDetailList))
        {
            List<CustomerSettleSheetDetail> list = new ArrayList<CustomerSettleSheetDetail>();
            for (CustomerSettleSheetDetail customerSettleSheetDetail : customerSettleSheetDetailList)
            {
                customerSettleSheetDetail.setSheetId(id);

                //随机生成组合商品编号
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String formattedDate = sdf.format(new Date());
                Random random = new Random();
                int randomNumber = 10000000 + random.nextInt(90000000); // 生成8位数随机数
                customerSettleSheetDetail.setId("160" + formattedDate+randomNumber);

                //实例化一个对账对象
                CustomerSettleCheckSheet customerSettleCheckSheet = new CustomerSettleCheckSheet();
                //赋值对账单状态
                customerSettleCheckSheet.setSettleStatus(Long.valueOf(1));
                //赋值对账单编号
                customerSettleCheckSheet.setId(customerSettleSheetDetail.getBizId());
                //调用对账修改接口
                customerSettleCheckSheetMapper.updateCustomerSettleCheckSheet(customerSettleCheckSheet);



                //调用查询对账详情接口
                CustomerSettleCheckSheet customerSettleCheckSheetDetails = customerSettleCheckSheetService.selectCustomerSettleCheckSheetById(customerSettleSheetDetail.getBizId());
                //循环遍历对账详情
                for(CustomerSettleCheckSheetDetail customerSettleCheckSheetDetail:customerSettleCheckSheetDetails.getCustomerSettleCheckSheetDetailList()){
                    //根据对账单详情中类型判断修改相对应的结算状态
                    if(customerSettleCheckSheetDetail.getBizType()==1){
                        //修改客户应收款单的结算状态
                        CustomerSettleFeeSheet customerSettleFeeSheet = new CustomerSettleFeeSheet();
                        customerSettleFeeSheet.setSettleStatus(Long.valueOf(1));
                        customerSettleFeeSheet.setId(customerSettleCheckSheetDetail.getBizId());
                        customerSettleFeeSheetMapper.updateCustomerSettleFeeSheet(customerSettleFeeSheet);
                        //根据对账的详情中的客户收款单id查询收款单详情
                        List<CustomerSettleFeeSheetDetail> FeeSheetDetail = customerSettleFeeSheetMapper.selectFeeSheetDetailIn(customerSettleCheckSheetDetail.getBizId());
                        //循环客户收款单详情修改结算状态
                        for(CustomerSettleFeeSheetDetail customerSettleFeeSheetDetail:FeeSheetDetail){
                            TblSaleOutSheet tblSaleOutSheet = new TblSaleOutSheet();
                            tblSaleOutSheet.setSettleStatus(Integer.valueOf(1));
                            tblSaleOutSheet.setId(customerSettleFeeSheetDetail.getOrderId());
                            tblSaleOutSheetMapper.updateTblSaleOutSheet(tblSaleOutSheet);
                        }
                    }
                    if(customerSettleCheckSheetDetail.getBizType()==2){
                        CustomerSettlePreSheet customerSettlePreSheet = new CustomerSettlePreSheet();
                        customerSettlePreSheet.setSettleStatus(Long.valueOf(1));
                        customerSettlePreSheet.setId(customerSettleCheckSheetDetail.getBizId());
                        customerSettlePreSheetMapper.updateCustomerSettlePreSheet(customerSettlePreSheet);

                        List<CustomerSettlePreSheetDetail> PreSheetDetail = customerSettlePreSheetMapper.selectPreSheetDetail(customerSettleCheckSheetDetail.getBizId());
                        for(CustomerSettlePreSheetDetail customerSettlePreSheetDetail:PreSheetDetail){
                            TblSaleReturn tblSaleReturn = new TblSaleReturn();
                            tblSaleReturn.setSettleStatus(Integer.valueOf(1));
                            tblSaleReturn.setId(customerSettlePreSheetDetail.getOrderId());
                            tblSaleReturnMapper.updateTblSaleReturn(tblSaleReturn);
                        }
                    }
                }


                list.add(customerSettleSheetDetail);
            }
            if (list.size() > 0)
            {
                customerSettleSheetMapper.batchCustomerSettleSheetDetail(list);
            }
        }
    }
}
